package com.benzhuo.widget.research.listview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * Created by WuWangchun on 2016/7/20.
 */
public class ZListView extends ListView{

    private View loadMoreView;
    public ZListView(Context context) {
        super(context);
    }

    public ZListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ZListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ZListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    protected void setLoadMoreView(final View loadMoreView){
        this.loadMoreView = loadMoreView;
        this.addFooterView(loadMoreView);	//设置列表底部视图
        loadMoreView.setVisibility(View.INVISIBLE);
        this.setOnScrollListener(new OnScrollListener() {
            private int visibleLastIndex;
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                int itemsLastIndex = getAdapter().getCount() - 1;	//数据集最后一项的索引
                int lastIndex = itemsLastIndex +1 ;				//加上底部的loadMoreView项
                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
                    //如果是自动加载,可以在这里放置异步加载数据的代码
                    loadMoreView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
//				this.visibleItemCount = visibleItemCount;
                visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
            }
        });
    }



}
